package cn.gorit.system.service.impl;

import cn.gorit.common.enums.ResultEnums;
import cn.gorit.common.enums.StateEnums;
import cn.gorit.common.lang.Result;
import cn.gorit.dto.system.MsgBoardDto;
import cn.gorit.entity.system.MsgBoard;
import cn.gorit.mapper.system.MsgBoardMapper;
import cn.gorit.system.service.MsgBoardService;
import cn.gorit.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @version v1.0
 * @Classname MsgBoardServiceImpl
 * @Description 留言板实现类
 * @Author CodingGorit
 * @Created on 2021/4/11 14:30
 * @IDLE IntelliJ IDEA
 **/
@Service
public class MsgBoardServiceImpl implements MsgBoardService {

    @Autowired
    private MsgBoardMapper mapper;

    @Override
    public Result<Object> add(MsgBoardDto dto) {
        mapper.insert(dto);
        return new Result<>(ResultEnums.ADD_SUCCESS.getCode(),ResultEnums.ADD_SUCCESS.getMsg(),"");
    }

    @Override
    public Result<Page<MsgBoard>> getBroadByPage(Page<MsgBoard> page) {
        List<MsgBoard> list = mapper.getBroadByPage(page);
        page.setList(list);
        Integer count = getBroadPageCount(page);
        page.setTotalCount(count);
        return new Result<>(ResultEnums.QUERY_SUCCESS.getCode(),ResultEnums.QUERY_SUCCESS.getMsg(),page);
    }

    @Override
    public Integer getBroadPageCount(Page<MsgBoard> page) {
        return mapper.getMsgBoardCountByPage(page);
    }

    @Override
    public Result<Object> updateMsgBoard(MsgBoard msgBoard) {
        mapper.update(msgBoard);
        return new Result<>(ResultEnums.UPDATE_SUCCESS.getCode(),ResultEnums.UPDATE_SUCCESS.getMsg(),"");
    }

    @Override
    public Result<MsgBoard> getMsgBoardById(Integer id) {
        return new Result<MsgBoard>(ResultEnums.QUERY_SUCCESS.getCode(),ResultEnums.QUERY_SUCCESS.getMsg(),mapper.getById(id));
    }

    @Override
    public Result<Object> updateStatus(Integer status, Integer id) {
        if (status.equals(1)) {
            mapper.updateStatus(StateEnums.ENABLED.getCode(),id);
        }
        if (status.equals(0)) {
            mapper.updateStatus(StateEnums.DISABLE.getCode(),id);
        }
        return new Result<>(ResultEnums.UPDATE_SUCCESS.getCode(),ResultEnums.UPDATE_SUCCESS.getMsg(),"");
    }
}
